package com.lx.user.web.socket;

import com.lx.user.web.respository.CodeStateRepository;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/websocket/{code}", configurator= EndpointConfigure.class)
@Component
public class QrCodeWebSocketHandler {

    @Resource
    private CodeStateRepository codeStateRepository;

    @OnOpen
    public void onOpen(Session session, @PathParam("code") String code) {
        codeStateRepository.addCode(code);
        System.out.println("webCode = " + code);
        // 这里可以获取到URL中的param参数
        System.out.println("WebSocket opened with param: " + code);
        codeStateRepository.addSession(code, session);
    }

}
